package;

import board.Dice;
import board.Roulette;
import board.Team;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.util.FlxMath;
import flixel.input.gamepad.LogitechButtonID;
import flixel.input.gamepad.PS4ButtonID;
import flixel.util.FlxColor;
import haxe.Utf8;
import flixel.tweens.FlxTween;
import sys.db.Connection;
import sys.db.SqLite;

/**
 * A FlxState which can be used for the actual gameplay.
 */
class VictoryState extends FlxState
{
	/**
	 * Function that is called up when to state is created to set it up. 
	 */
	var text1:FlxText;
	var text2:FlxText;
	var text3:FlxText;
	var text4:FlxText;
	var text5:FlxText;
	var high:FlxText;
	var scores:Array<Int>;
	var time:Float;
	var reset:Float;
	override public function create():Void
	{
		super.create();
		FlxG.camera.bgColor = FlxColor.ROYAL_BLUE;
		FlxG.camera.fade(FlxColor.BLACK, 1, true, onFadeIn);
		Reg.score1 += Reg.scoreAdd1;
		Reg.score2 += Reg.scoreAdd2;
		Reg.score3 += Reg.scoreAdd3;
		Reg.score4 += Reg.scoreAdd4;
		
		text1 = new FlxText(0, 20, 800, "Y EL GANADOR ES...");
		text1.setFormat("title", 100, FlxColor.GOLDENROD, "center", FlxText.BORDER_OUTLINE);
		add(text1);
		
		text2 = new FlxText(0, 240, 800, "AZUL: "+ Std.string(Reg.score1));
		text2.setFormat("title", 70, FlxColor.BLUE, "center", FlxText.BORDER_OUTLINE);
		add(text2);
		text2.scale.set(0, 0);
		
		text3 = new FlxText(0, 310, 800, "ROJO: "+ Std.string(Reg.score2));
		text3.setFormat("title", 70, FlxColor.RED, "center", FlxText.BORDER_OUTLINE);
		add(text3);
		text3.scale.set(0, 0);
		
		text4 = new FlxText(0, 380, 800, "VERDE: "+ Std.string(Reg.score3));
		text4.setFormat("title", 70, FlxColor.GREEN, "center", FlxText.BORDER_OUTLINE);
		add(text4);
		text4.scale.set(0, 0);
		
		text5 = new FlxText(0, 450, 800, "AMARILLO: "+ Std.string(Reg.score4));
		text5.setFormat("title", 70, FlxColor.YELLOW, "center", FlxText.BORDER_OUTLINE);
		add(text5);
		text5.scale.set(0, 0);
		
		scores = [Reg.score1, Reg.score2, Reg.score3, Reg.score4];
		scores.sort(greater);			
		var score = scores.pop();
		if (score == Reg.score1)
		{
			high = text2;
			text2.y = 240;
			Reg.score1 = -1;
		}
		else if (score == Reg.score2)
		{
			high = text3;
			text3.y = 240;
			Reg.score2 = -1;
		}
		else if (score == Reg.score3)
		{
			high = text4;
			text4.y = 240;
			Reg.score3 = -1;
		}
		else if (score == Reg.score4)
		{
			high = text5;
			text5.y = 240;
			Reg.score4 = -1;
		}
			
		var score = scores.pop();
		if (score == Reg.score1)
		{
			Reg.score1 = -1;
			text2.y = 310;
		}
		else if (score == Reg.score2)
		{
			Reg.score2 = -1;
			text3.y = 310;
		}
		else if (score == Reg.score3)
		{
			Reg.score3 = -1;
			text4.y = 310;
		}
		else if (score == Reg.score4)
		{
			Reg.score4 = -1;
			text5.y = 310;
		}

		var score = scores.pop();
		if (score == Reg.score1)
		{
			Reg.score1 = -1;
			text2.y = 380;
		}
		else if (score == Reg.score2)
		{
			Reg.score2 = -1;
			text3.y = 380;
		}
		else if (score == Reg.score3)
		{
			Reg.score3 = -1;
			text4.y = 380;
		}
		else if (score == Reg.score4)
		{
			Reg.score4 = -1;
			text5.y = 380;
		}
			
		var score = scores.pop();
		if (score == Reg.score1)
		{
			Reg.score1 = -1;
			text2.y = 450;
		}
		else if (score == Reg.score2)
		{
			Reg.score2 = -1;
			text3.y = 450;
		}
		else if (score == Reg.score3)
		{
			Reg.score3 = -1;
			text4.y = 450;
		}
		else if (score == Reg.score4)
		{
			Reg.score4 = -1;
			text5.y = 450;
		}
			
		time = 3.7;
		reset = 20;
		FlxG.sound.play("assets/sounds/drumroll.wav");
	}
	
	private function greater(a:Int, b:Int):Int
	{
		if (a == b)
			return 0;
		if (a > b)
			return 1;
		else
			return -1;
	}
	private function onFadeIn():Void
	{
		
	}
	
	/**
	 * Function that is called when this state is destroyed - you might want to 
	 * consider setting all objects this state uses to null to help garbage collection.
	 */
	override public function destroy():Void
	{
		super.destroy();
	}

	/**
	 * Function that is called once every frame.
	 */
	override public function update():Void
	{
		super.update();
		time -= FlxG.elapsed;
		reset -= FlxG.elapsed;
		if (time < 0)
		{
			var tween = FlxTween.tween(text2.scale,{x:1,y:1},1);
			var tween = FlxTween.tween(text3.scale,{x:1,y:1},1);
			var tween = FlxTween.tween(text4.scale,{x:1,y:1},1);
			var tween = FlxTween.tween(text5.scale, { x:1, y:1 }, 1, { complete:tweenEnd } );
			time = 1000;
		}
		if (reset < 0)
		{
			FlxG.camera.fade(FlxColor.BLACK,1,false,onFade);
		}
		
	}	
	
	private function onFade():Void
	{
		Reg.score1 = 0;
		Reg.score2 = 0;
		Reg.score3 = 0;
		Reg.score4 = 0;
		Reg.carcel1 = 0;
		Reg.carcel2 = 0;
		Reg.carcel3 = 0;
		Reg.carcel4 = 0;
		Reg.multiplier1 = 1;
		Reg.multiplier2 = 1;
		Reg.multiplier3 = 1;
		Reg.multiplier4 = 1;
		Reg.position1 = 0;
		Reg.position2 = 0;
		Reg.position3 = 0;
		Reg.position4 = 0;
		Reg.rondas = 5;
		Reg.scoreAdd1 = 0;
		Reg.scoreAdd2 = 0;
		Reg.scoreAdd3 = 0;
		Reg.scoreAdd4 = 0;
		var cnx = sys.db.Sqlite.open("assets/TestDB.sqlite");
		var rset = cnx.request("DELETE FROM Partidas");
		try 
		{
			cnx.request("INSERT INTO Partidas (score1,score2,score3,score4,carcel1,carcel2,carcel3,carcel4,multiplier1,multiplier2,multiplier3,multiplier4,posicion1,posicion2,posicion3,posicion4,rondas) VALUES (" +
			Reg.score1 + "," +  Reg.score2 + "," + Reg.score3 + "," + Reg.score4 + "," +
			Reg.carcel1 + "," + Reg.carcel2 + "," + Reg.carcel3 + "," + Reg.carcel4 + "," +
			Reg.multiplier1 + "," + Reg.multiplier2 + "," + Reg.multiplier3 + "," + Reg.multiplier4 + "," +
			Reg.position1 + "," + Reg.position2 + "," + Reg.position3 + "," + Reg.position4 + "," + Reg.rondas + ")");
		} 
		catch( msg : String ) {
			trace("Error occurred: " + msg);
		}
		var rset = cnx.request("DELETE FROM ScoreAdd");
		try 
		{
			cnx.request("INSERT INTO ScoreAdd (add1,add2,add3,add4) VALUES (" +
			Reg.scoreAdd1 + "," +  Reg.scoreAdd2 + "," + Reg.scoreAdd3 + "," + Reg.scoreAdd4 + ")");
		} 
		catch( msg : String ) {
			trace("Error occurred: " + msg);
		}
		cnx.close();
		FlxG.switchState(new LogoState());
	}
	private function tweenEnd(tween2:FlxTween):Void 
	{
		var tween = FlxTween.tween(high.scale, { x:1.3, y:1.3 }, 1, { type:FlxTween.PINGPONG } );
	}
}